package com.yilian.xlmusic.android.utils.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.yilian.xlmusic.android.R;
import com.yilian.xlmusic.android.utils.port.ClickListener;

public class CollectCheckBox extends RelativeLayout {
    private ImageView checkBox;
    private RelativeLayout collect;
    private boolean checked;
    private Context context;
    private ClickListener listener;
    public CollectCheckBox(Context context) {
        this(context, null);
    }

    public CollectCheckBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CollectCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);

    }

    @SuppressLint("ResourceType")
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View.inflate(context, R.layout.view_check_bos, this);
        checkBox = this.findViewById(R.id.collect_ima);
        collect = this.findViewById(R.id.collect);
        this.context = context;
        collect.setOnClickListener(v -> {
            listener.click();
        });

    }

    public void setCheckBox(boolean checked) {
        this.checked = checked;
        changeChecked(checked);

    }

    public boolean getCheckBox() {
        return checked;
    }

    /**
     * 改变按压状态
     */
    private void changeChecked(boolean checked){
        defaultChecked(checked);
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.collect);
        checkBox.startAnimation(animation);

    }

    /**
     *
     * @param checked 默认状态，避免刚渲染动画
     */
    public void defaultChecked(boolean checked){
        this.checked = checked;
        if (checked){
            checkBox.setImageResource(R.mipmap.collection_btn_pre_20);
        }else {
            checkBox.setImageResource(R.mipmap.collection_btn_20);
        }
    }

    /**
     *
     * @param listener 收藏监听
     */
    public void bindListener(ClickListener listener){
        this.listener = listener;
    }
}
